﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : tianteng
// Created          : 03-16-2018
//
// Last Modified By : tianteng
// Last Modified On : 03-16-2018
// ***********************************************************************
// <copyright file="AddGroupCmd.cs" company="北京智帆高科科技有限公司">
//     Copyright ©  2017
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Linq;
using System.Windows.Forms;
using ZFGK.Addins;
using ZFGK.Delegates;
using ZFGK.Utility;
using ZFGK.DevExprss.Forms;
using ZFGK.osgEarth.UI;
using OSS.BIM.Model;
using OSS.BIM.Business;
using OSS.BIM.Business.Utility;
using OSS.BIM.UI.PropertyGrids;
using OSS.BIM.Utility;
using ZfPlatform.Utility;

namespace OSS.BIM.Commands.GroupCmds
{
    /// <summary>
    /// Class AddGroupCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class GroupAddCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var userProject = cmdData.Tag as Project;
            if (userProject == null)
                userProject = CurUserGroup.Instance.Project;
            if (userProject == null)
            {
                ZfMessageUtil.ShowError("当前项目为空，添加失败!");
                return CmdResult.Cancel;
            }
            var userGroup = ZfCacheUtil.valueCache.LaodCache<ModelGrope>();
            if (userGroup == null)
            {
                userGroup = new ModelGrope();
                userGroup.Name = "默认分组名称";
            }
            var form = cmdData.ViewForm as OsgEarthViewForm;
            var userGroupBinding = new ModelGropeBinding(userGroup);
            var propertyGridAddEditForm = new DvPropertyGridEditForm("添加分组", userGroupBinding);
            propertyGridAddEditForm.Icon = form.Icon;
            propertyGridAddEditForm.ValueValidated += delegate(object sender, ValueValidatedArgs args)
            {
                userGroup.Name = userGroup.Name.Trim();
                if (string.IsNullOrEmpty(userGroup.Name))
                {
                    args.ErrorMessage.AppendLine("分组名称殂错误！");
                    return false;
                }
                // 缓存记忆值
                ZfCacheUtil.valueCache.SaveCache(userGroup);
                return true;
            };
            if (propertyGridAddEditForm.ShowDialog(cmdData.ViewForm as OsgEarthViewForm) != DialogResult.OK)
                return CmdResult.Cancel;

            userGroup.ProjectId = userProject.ProjectId;
            userGroup.AttachAll();
            if (!ModelGropeDbUtil.Insert(userGroup))
            {
                ZfMessageUtil.ShowError("更新数据库失败!");
                return CmdResult.Cancel;
            }
            userGroup.AddOrUpdateTreeNode();
            return CmdResult.Succeed;
        }
    }
}
